package mx.com.mindbits.bandpay.fragments;

import com.actionbarsherlock.app.SherlockFragment;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import mx.com.mindbits.bandpay.R;
import mx.com.mindbits.bandpay.TransactionDetailActivity;
import mx.com.mindbits.bandpay.dao.BandPayDAO;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.dao.transactions;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TransactionMapFragment extends SherlockFragment{
	
	private long idTransaction = -1;
	private Context context = null;
	private BandPayDAO bAndPayDAO;
	
	GoogleMap googleMap;
	LatLng salePoint; 

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		TransactionDetailActivity transDetailActivity = (TransactionDetailActivity) activity; 

		Log.d(ConstantsValues.TAG_TRANS_DETAIL_MAP, "OBJETO activity " + transDetailActivity.getIdRx() );
		idTransaction = transDetailActivity.getIdRx();
		context = transDetailActivity.getBaseContext();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		View view = inflater.inflate(R.layout.fragment_map, container, false);
		setLatLon();
		createMap(view);
		return view;
	}
	
	private void setLatLon(){
		bAndPayDAO = new BandPayDAO(context);
		transactions transaction = bAndPayDAO.getTransactionById(idTransaction);
		
		double lat = (transaction.getLat());
		double lon = (transaction.getLon());
		salePoint = new LatLng(lat, lon);
		Log.d(ConstantsValues.TAG_TRANS_DETAIL_MAP, "OBJETO transaccion: " + transaction);
		Log.d(ConstantsValues.TAG_TRANS_DETAIL_MAP, "MAP FRAGMENT Latitud: " + lat + " | Longitud: " + lon);
	}
	
	private void createMap(View view){
		
		SupportMapFragment supportMapFragment;
	    supportMapFragment = (SupportMapFragment) (getSherlockActivity().
	    		getSupportFragmentManager().findFragmentById(R.id.map));	
	    
	    googleMap = supportMapFragment.getMap();
	    
	    Marker saleMarker = googleMap.addMarker(new MarkerOptions().
	    		position(salePoint).title(getResources().getString(R.string.transaccion_mark)));
	    
	    Log.i("MAPA", ":" + saleMarker.getPosition());
	    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(salePoint, ConstantsValues.ZOOM_MAP));
	   
	    googleMap.animateCamera(CameraUpdateFactory.zoomTo(ConstantsValues.ZOOM_CAMERA), ConstantsValues.DURATION_ANIMATE_MS, null);
	    	   	    
	}
	
	
}
